package bean;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.CheckBox;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import lombok.Data;
import utils.fileHandler.FileHandler;
import utils.wallpaperHandler.WallpaperHandler;

@Data
public class ImageInfo {
    public static final int SimpleImage = 1;//jpg,jpeg,png,bmp
    public static final int GifImage = 2;//gif
    public static final int VideoImage = 3;//mp4,flv,flac,mov...
    private String imageAbsolutePath;
    private String imagePath;
    private ImageView imageView;
    private Image image;
    private Boolean checkSelect;
    private CheckBox checkBox;
    private int type;

    public ImageInfo(String imagePath) {
        this.imagePath = imagePath;
        this.imageAbsolutePath = imagePath.replace("file:", "");
        checkSelect = Boolean.FALSE;
        this.image = new Image(imagePath);
        imageView = new ImageView(image);
        checkBox = new CheckBox();
        checkBox.setSelected(false);
        checkBox.setIndeterminate(false);
        checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observableValue, Boolean old_val, Boolean new_val) {
                checkSelect = new_val;
            }
        });
        checkType();
    }

    public ImageInfo(Image image) {
        this.image = image;
        imagePath = image.getUrl();
        this.imageAbsolutePath = imagePath.replace("file:", "");
        checkSelect = Boolean.FALSE;
        imageView = new ImageView(image);
        checkBox = new CheckBox();
        checkBox.setSelected(false);
        checkBox.setIndeterminate(false);
        checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observableValue, Boolean old_val, Boolean new_val) {
                checkSelect = new_val;
                System.out.println(new_val);
            }
        });
        checkType();
    }

    public ImageInfo(String imagePath, int type) {
        this.type = type;
        this.imagePath = imagePath;
        this.imageAbsolutePath = imagePath.replace("file:", "");
        checkSelect = Boolean.FALSE;
        this.image = new Image(imagePath);
        imageView = new ImageView(image);
        checkBox = new CheckBox();
        checkBox.setSelected(false);
        checkBox.setIndeterminate(false);
        checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observableValue, Boolean old_val, Boolean new_val) {
                checkSelect = new_val;
            }
        });
    }

    public ImageInfo(Image image, int type) {
        this.type = type;
        this.image = image;
        imagePath = image.getUrl();
        this.imageAbsolutePath = imagePath.replace("file:", "");
        checkSelect = Boolean.FALSE;
        imageView = new ImageView(image);
        checkBox = new CheckBox();
        checkBox.setSelected(false);
        checkBox.setIndeterminate(false);
        checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observableValue, Boolean old_val, Boolean new_val) {
                checkSelect = new_val;
                System.out.println(new_val);
            }
        });
    }

    public void setCheckVT() {
        checkBox.setSelected(true);
        checkSelect = true;
    }

    public void setCheckVF() {
        checkBox.setSelected(false);
        checkSelect = false;
    }

    private void checkType() {
        String path=imagePath.replace("file:","");
        if (WallpaperHandler.gethDImagePath(path)==null){
            path=WallpaperHandler.gethDVideoPath(path);
        }else {
            path=WallpaperHandler.gethDImagePath(path);
        }
        String fileType = FileHandler.getFileType(path);
        switch (fileType) {
            case "jpg":
            case "png":
            case "jpeg":
            case "bmp": {
                type = ImageInfo.SimpleImage;
                break;
            }
            case "gif":{
                type= ImageInfo.GifImage;
            }
            case "mp4":{
                type= ImageInfo.VideoImage;
            }
        }
    }
}